package com.wn.service;

import com.common.base.BaseService;
import com.common.entity.PageParam;
import com.common.entity.Rsp;
import com.common.entity.TravelContent;
import com.common.entity.TravelReviews;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.mapper.TravelReviewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;
@Service
public class TravelReviewsService implements BaseService<TravelReviews> {
    @Autowired
    TravelReviewsMapper travelReviewsMapper;

    @Override
    public PageInfo<TravelReviews> searchSomeByPage(PageParam<TravelReviews> pageParam) {
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<TravelReviews> list=travelReviewsMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<TravelReviews> pageInfo = new PageInfo<TravelReviews>(list);

            return pageInfo;
        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<TravelReviews> list=travelReviewsMapper.searchSomeByPage(pageParam.getModel());
            PageInfo<TravelReviews> pageInfo = new PageInfo<TravelReviews>(list);

            return pageInfo;

        }
    }

    @Override
    public TravelReviews getOneById(String id) {
        return travelReviewsMapper.getOneById(id);
    }

    @Override
    public Rsp addOne(TravelReviews travelReviews) {
        travelReviews.setReviewsId(UUID.randomUUID().toString().replaceAll("-", ""));
//        travelContent.setCreatetime(new Date());

        return travelReviewsMapper.addOne(travelReviews)==1?Rsp.success("添加成功"):Rsp.fail().msg("添加失败");

    }

    @Override
    public Rsp update(TravelReviews travelReviews) {
        return travelReviewsMapper.update(travelReviews)==1?Rsp.success("修改成功"):Rsp.fail().msg("修改失败");
    }

    @Override
    public Rsp deleteLogical(TravelReviews travelReviews) {
        travelReviews.setDeleted(1);
        return travelReviewsMapper.update(travelReviews)==1?Rsp.success("删除成功"):Rsp.fail().msg("删除失败");
    }
}
